#include "ActionAgentBackEnd.h"
#include "ActionBase.h"
#include "context.h"
#include "util.h"
#include "streamdata.h"
#include "Md5.h"
#include "json.h"
#include "1650_agentbackend.pb.h"


static bool __init__ = ActionAgentBackEnd::Init();

bool ActionAgentBackEnd::Init()
{
    Ctx.msg_router.BindHttpHandle("post", "/agentbackend/refresh", ActionAgentBackEnd::HandleRefreshConfig);
    Ctx.msg_router.BindHttpHandle("post", "/agentbackend/server-rewrite", ActionAgentBackEnd::HandleServerRewrite);

    return true;
}

void ActionAgentBackEnd::HandleRefreshConfig(HttpData&& http_data)
{
    log_debug("enter,request_id:%d", http_data.request_id);
    int err = 0;
    std::stringstream body;
    do
    {
		std::int32_t sv_type = 0;
		std::int32_t svid = 0;
        if (http_data.sub_paths.size() == 3)
        {
            sv_type = atoi(http_data.sub_paths[2].c_str());
        }
        else if (http_data.sub_paths.size() == 4)
        {
            sv_type = atoi(http_data.sub_paths[2].c_str());
            svid = atoi(http_data.sub_paths[3].c_str());
        }
        else
        {
            body << "path:" << http_data.fullpath << " failed, format:/agentbackend/refresh/$svtype or format:/agentbackend/refresh/$svtype/$svid";
            err = 2;
            break;
        }

        agentbackend::RefreshConfigNotify notify;
        if (!CTools::FromJson(notify, http_data.body))
        {
            body << "parser agentbackend::RefreshConfigNotify failed, url:" << http_data.target << ", body:" << http_data.body;
            err = 1;
            break;
        }
		if (svid == 0)
		{
            Ctx.host->MtBroadcastMessageDirect(agentbackend::AGENT_BACKEND_MSG_ID_REFRESH_CONFIG, &notify, sv_type);
		}
		else
		{
			Ctx.host->SendtoOneSrv(agentbackend::AGENT_BACKEND_MSG_ID_REFRESH_CONFIG, &notify, sv_type, svid);
		}
        body << "ok";
    } while (false);
    ResponseHttp(http_data.request_id, err, body.str());
}

void ActionAgentBackEnd::HandleServerRewrite(HttpData&& http_data)
{
    log_debug("enter,request_id:%d", http_data.request_id);
    int err = 0;
    std::stringstream body;
    do
    {
		std::int32_t sv_type = 0;
		std::int32_t svid = 0;
        if (http_data.sub_paths.size() == 4)
        {
            sv_type = atoi(http_data.sub_paths[2].c_str());
            svid = atoi(http_data.sub_paths[3].c_str());
        }
        else
        {
            body << "path:" << http_data.fullpath << " failed, format:/agentbackend/refresh/$svtype or format:/agentbackend/refresh/$svtype/$svid";
            err = 2;
            break;
        }
		if (svid == 0)
		{
            body << "path:" << http_data.fullpath << " failed, svid is 0";
            err = 3;
            break;
		}

	    agentbackend::ServerRetireNotify notify;
        if (!CTools::FromJson(notify, http_data.body))
        {
            body << "parser agentbackend::ServerRetireNotify failed, url:" << http_data.target << ", body:" << http_data.body;
            err = 1;
            break;
        }
		{
			Ctx.host->SendtoOneSrv(agentbackend::AGENT_BACKEND_MSG_ID_SERVER_RETIRE, &notify, sv_type, svid);
		}
        body << "ok";
    } while (false);
    ResponseHttp(http_data.request_id, err, body.str());
}






















